package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.bo.wx.WxOrderCommentBo;
import com.cskaoyan.bean.bo.wx.WxOrderSubmitBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.order.WxOrderSubmitVo;
import com.cskaoyan.config.order.WxOrderHandelOptionConfig;
import com.cskaoyan.bean.vo.wx.order.OrderDetailVo;
import com.cskaoyan.bean.vo.wx.order.OrderListVo;
import com.cskaoyan.config.order.WxOrderStatusConfig;
import com.cskaoyan.mapper.*;
import com.cskaoyan.utils.DateUtil;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @Author: zhang
 * @Date: 2022/11/23 9:00
 * @Since: jdk1.8
 */
@Service
@EnableConfigurationProperties(WxOrderStatusConfig.class)
public class WxOrderServiceImpl implements WxOrderService {
    private static Properties property = null;

    static {
        InputStream resource = WxOrderServiceImpl.class.getClassLoader().getResourceAsStream("SystemValue.properties");
        try {
            property = new Properties();
            property.load(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;
    @Autowired
    MarketCartMapper marketCartMapper;
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    CartService cartService;
    @Autowired
    WxOrderHandelOptionConfig handelOptionConfig;
    @Autowired
    WxOrderStatusConfig wxOrderStatusConfig;

    /**
     * 列出所有的 订单
     *
     * @param userId   用户id
     * @param showType 订单状态
     * @param page     页码
     * @param limit    限制
     * @return CommonData<OrderListVo>
     */
    @Override
    public CommonData<OrderListVo> list(Integer userId, Integer showType, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<OrderListVo> orderListVos = this.getOrderListVos(userId, showType);
        PageInfo<OrderListVo> pageInfo = new PageInfo<>();
        if (orderListVos != null) {
            pageInfo = new PageInfo<>(orderListVos);
        }
        return CommonData.data(pageInfo);
    }

    /**
     * 订单详情
     *
     * @param userId  用户id
     * @param orderId 订单id
     * @return 订单信息
     */
    @Override
    public OrderDetailVo detail(Integer userId, Integer orderId) {
        // 查询需要的订单信息
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        if (!StringUtil.isEmpty(String.valueOf(userId))) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtil.isEmpty(String.valueOf(orderId))) {
            criteria.andIdEqualTo(orderId);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketOrder> orderList = marketOrderMapper.selectByExample(marketOrderExample);
        MarketOrder order = orderList.remove(0);
        // 查询订单中的商品信息
        List<MarketOrderGoods> orderGoodsList = this.getOrderGoodsList(orderId);
        // 封装返回参数
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        OrderDetailVo.OrderInfoBean orderInfo = orderDetailVo.getOrderInfo();
        orderInfo.setConsignee(order.getConsignee());
        orderInfo.setAddress(order.getAddress());
        orderInfo.setAddTime(order.getAddTime());
        orderInfo.setOrderSn(order.getOrderSn());
        orderInfo.setMobile(order.getMobile());
        orderInfo.setMessage(order.getMessage());
        Integer orderStatus = order.getOrderStatus();
        orderInfo.setOrderStatusText(getOrderStatusText(orderStatus));
        orderInfo.setAftersaleStatus(order.getAftersaleStatus());
        orderInfo.setId(orderId);
        orderInfo.setGoodsPrice(order.getGoodsPrice());
        orderInfo.setCouponPrice(order.getCouponPrice());
        orderInfo.setFreightPrice(order.getFreightPrice());
        orderInfo.setActualPrice(order.getActualPrice());
        // 获取操作值
        String setValue = WxOrderServiceImpl.property.getProperty(orderStatus.toString());
        WxOrderHandelOptionConfig wxOrderHandelOptionConfig = this.getHandleOption(setValue);
        orderInfo.setHandleOption(wxOrderHandelOptionConfig);
        orderDetailVo.setOrderGoods(orderGoodsList);
        return orderDetailVo;
    }

    /**
     * 取消订单
     *
     * @param userId  用户id
     * @param orderId 订单id
     * @return 影响的行数
     */
    @Override
    public void cancel(Integer userId, Integer orderId) {
        Integer cancel = wxOrderStatusConfig.getCancelByUser();
        changeOrderStatus(userId, orderId, cancel);
    }

    /**
     * 申请退款
     *
     * @param userId  用户id
     * @param orderId 订单id
     * @return 影响的行数
     */
    @Override
    public void refund(Integer userId, Integer orderId) {
        Integer refund = wxOrderStatusConfig.getRefund();
        changeOrderStatus(userId, orderId, refund);
    }

    /**
     * 确认收货
     *
     * @param userId  用户id
     * @param orderId 订单id
     */
    @Override
    public void confirm(Integer userId, Integer orderId) {
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        if (!StringUtil.isEmpty(String.valueOf(userId))) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtil.isEmpty(String.valueOf(orderId))) {
            criteria.andIdEqualTo(orderId);
        }
        Integer status = wxOrderStatusConfig.getConfirmByUser();
        Date date = DateUtil.getDate();
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setOrderStatus(status);
        marketOrder.setConfirmTime(date);
        marketOrder.setUpdateTime(date);
        marketOrderMapper.updateByExampleSelective(marketOrder, marketOrderExample);
    }

    /**
     * 去付款
     *
     * @param userId  用户id
     * @param orderId 订单id
     */
    @Override
    public void prepay(Integer userId, Integer orderId) {
        // 修改订单表的状态
        Integer status = wxOrderStatusConfig.getPaied();
        String payId = StringUtil.getRandomNum(16);
        Date date = DateUtil.getDate();
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        if (!StringUtil.isEmpty(String.valueOf(userId))) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtil.isEmpty(String.valueOf(orderId))) {
            criteria.andIdEqualTo(orderId);
        }
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setOrderStatus(status);
        marketOrder.setPayId(payId);
        marketOrder.setShipTime(date);
        marketOrder.setUpdateTime(date);
        marketOrderMapper.updateByExampleSelective(marketOrder, marketOrderExample);
    }

    @Override
    public MarketOrderGoods goods(Integer orderId, Integer goodsId) {
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        if (!StringUtils.isEmpty(String.valueOf(orderId))) {
            criteria.andOrderIdEqualTo(orderId);
        }
        if (!StringUtil.isEmpty(String.valueOf(goodsId))) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        return marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample).remove(0);
    }

    @Override
    public void comment(WxOrderCommentBo wxOrderCommentBo) {
        
    }

    @Override
    @Transactional
    public WxOrderSubmitVo submit(Integer userId, WxOrderSubmitBo wxOrderSubmitBo) {
        Integer userCouponId = wxOrderSubmitBo.getUserCouponId();
        // 修改coupon_user表中的信息
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria couponUserCriteria = marketCouponUserExample.createCriteria();
        if (!StringUtil.isEmpty(String.valueOf(userCouponId))) {
            couponUserCriteria.andIdEqualTo(userCouponId);
        }
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setStatus((short) 1);
        Date date = DateUtil.getDate();
        marketCouponUser.setUsedTime(date);
        marketCouponUser.setDeleted(true);
        marketCouponUserMapper.updateByExampleSelective(marketCouponUser, marketCouponUserExample);
        // 然后根据useCouponId找到useId
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        MarketCouponUser couponUser = marketCouponUsers.remove(0);
        Integer marketUserId = couponUser.getUserId();
        // 然后通过useId修改购物车
        MarketCartExample marketCartExample = new MarketCartExample();
        MarketCartExample.Criteria cartCriteria = marketCartExample.createCriteria();
        if (!StringUtil.isEmpty(String.valueOf(marketUserId))) {
            cartCriteria.andUserIdEqualTo(marketUserId);
        }
        marketCartMapper.deleteByExample(marketCartExample);
        // 订单表中增加订单
        MarketOrder order = new MarketOrder();
        order.setUserId(userId);
        order.setOrderSn(getOderSn(6));
        Integer paied = wxOrderStatusConfig.getPaied();
        order.setOrderStatus(paied);
        order.setAftersaleStatus(0);
        Integer addressId = wxOrderSubmitBo.getAddressId();
        MarketAddress address = marketAddressMapper.selectByPrimaryKey(addressId);
        order.setConsignee(address.getName());
        order.setMobile(address.getTel());
        order.setAddress(address.getProvince() + address.getCity() + address.getCounty() + address.getAddressDetail());
        order.setMessage(wxOrderSubmitBo.getMessage());
        // 设置订单商品总价格，先查询购物车中选中的商品信息
        List<MarketCart> marketCarts = cartService.queryUidAndChecked(userId);
        List<MarketOrderGoods> marketOrderGoods = new ArrayList<>();
        BigDecimal goodsPrice = new BigDecimal(0);
        for (MarketCart marketCart : marketCarts) {
            // 增加订单商品表中的数据
            MarketOrderGoods orderGoods = new MarketOrderGoods();
            orderGoods.setOrderId(order.getId());
            orderGoods.setGoodsId(marketCart.getGoodsId());
            orderGoods.setGoodsName(marketCart.getGoodsName());
            orderGoods.setGoodsSn(marketCart.getGoodsSn());
            orderGoods.setProductId(marketCart.getProductId());
            orderGoods.setNumber(marketCart.getNumber().shortValue());
            orderGoods.setPrice(BigDecimal.valueOf(marketCart.getPrice()));
            orderGoods.setSpecifications(marketCart.getSpecifications());
            orderGoods.setPicUrl(marketCart.getPicUrl());
            orderGoods.setComment(0);
            orderGoods.setAddTime(date);
            orderGoods.setUpdateTime(date);
            orderGoods.setDeleted(false);
            int id = marketOrderGoodsMapper.insertSelective(orderGoods);
            goodsPrice=goodsPrice.add(orderGoods.getPrice());
            marketOrderGoods.add(orderGoods);
        }
        order.setGoodsPrice(goodsPrice);
        double doubleValue = goodsPrice.doubleValue();
        /*if (doubleValue > 88) {
            order.setFreightPrice(new BigDecimal(0));
        } else {
            order.setFreightPrice(new BigDecimal(12));
        }*/
        order.setFreightPrice(new BigDecimal(0));
        Integer couponId = wxOrderSubmitBo.getCouponId();
        MarketCoupon coupon = marketCouponMapper.selectByPrimaryKey(couponId);
        order.setCouponPrice(coupon.getDiscount());
        // order.setIntegralPrice(new BigDecimal(0));
        // order.setGrouponPrice(new BigDecimal(0));
        order.setActualPrice(this.getActualPrice(order, marketOrderGoods));
        order.setOrderPrice(order.getActualPrice().add(order.getIntegralPrice()));
        order.setPayId(StringUtil.getRandomNum(16));
        order.setPayTime(date);
        order.setAddTime(date);
        order.setDeleted(false);
        int orderId = marketOrderMapper.insertSelective(order);
        return new WxOrderSubmitVo(orderId, 0);
    }

    /**
     * 删除订单
     *
     * @param userId  用户id
     * @param orderId 订单id
     */
    @Override
    public void delete(Integer userId, Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setDeleted(true);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    /**
     * 获得订单编号
     *
     * @param len 长度
     * @return 订单编号
     */
    private String getOderSn(int len) {
        Date date = DateUtil.getDate();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(date);
        String[] split = format.split("-");
        StringBuilder orderSn = new StringBuilder();
        for (String s : split) {
            orderSn.append(s);
        }
        String randomNum = StringUtil.getRandomNum(len);
        return orderSn.append(randomNum).toString();
    }

    /**
     * 改变订单的状态
     *
     * @param userId  用户id
     * @param orderId 订单id
     * @param status  订单状态
     */
    private void changeOrderStatus(Integer userId, Integer orderId, Integer status) {
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        if (!StringUtil.isEmpty(String.valueOf(userId))) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtil.isEmpty(String.valueOf(orderId))) {
            criteria.andIdEqualTo(orderId);
        }
        MarketOrder marketOrder = marketOrderMapper.selectOrderById(String.valueOf(orderId));
        marketOrder.setOrderStatus(status);
        marketOrderMapper.updateByExample(marketOrder, marketOrderExample);
    }

    /**
     * 获取订单状态的内容
     *
     * @param orderStatus 订单状态值
     * @return 订单状态的内容
     */
    private String getOrderStatusText(Integer orderStatus) {
        switch (orderStatus) {
            case 101:
                return "未付款";
            case 102:
                return "已取消";
            case 103:
                return "已取消(系统)";
            case 201:
                return "已付款";
            case 202:
                return "订单取消,退款中";
            case 203:
                return "已退款";
            case 301:
                return "已发货";
            case 401:
            case 402:
                return "已收货";
            default:
        }
        return "订单状态错误";
    }

    /**
     * 获取要返回的订单信息
     *
     * @param userId   用户id
     * @param showType 订单状态
     * @return 订单中的商品信息
     */
    private List<OrderListVo> getOrderListVos(Integer userId, Integer showType) {
        // TODO showType      0 --> 全部   1 --> 待付款   2--> 待发货   3 --> 待收货   4 --> 待评价
        List<MarketOrder> orderList = null;
        switch (showType) {
            case 0:
                // 查询所有没删除的订单
                MarketOrderExample marketOrderExample = new MarketOrderExample();
                MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
                if (!StringUtil.isEmpty(String.valueOf(userId))) {
                    criteria.andUserIdEqualTo(userId);
                }
                criteria.andDeletedEqualTo(false);
                orderList = marketOrderMapper.selectByExample(marketOrderExample);
                break;
            case 1:
                Integer nopay = wxOrderStatusConfig.getNopay();
                orderList = getOrderList(userId, nopay);
                break;
            case 2:
                Integer paied = wxOrderStatusConfig.getPaied();
                orderList = getOrderList(userId, paied);
                break;
            case 3:
                Integer deliver = wxOrderStatusConfig.getDeliver();
                orderList = getOrderList(userId, deliver);
                break;
            case 4:
                Integer confirm = wxOrderStatusConfig.getConfirmByUser();
                orderList = getOrderList(userId, confirm);
                break;
            default:
        }
        ArrayList<OrderListVo> orderListVos = new ArrayList<>();
        List<MarketOrderGoods> orderGoodsList;
        if (orderList != null) {
            for (MarketOrder order : orderList) {
                Integer orderId = order.getId();
                orderGoodsList = getOrderGoodsList(orderId);
                // 得到要返回的值
                orderListVos.add(this.setOrderListVo(order, orderGoodsList));
            }
            return orderListVos;
        }
        return null;
    }


    /**
     * 查询订单
     *
     * @param userId 用户id
     * @param status 订单状态
     * @return 查询到的订单
     */
    private List<MarketOrder> getOrderList(Integer userId, Integer status) {
        List<MarketOrder> orderList;
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        if (!StringUtil.isEmpty(String.valueOf(userId))) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtil.isEmpty(String.valueOf(status))) {
            criteria.andOrderStatusEqualTo(status.shortValue());
        }
        criteria.andDeletedEqualTo(false);
        orderList = marketOrderMapper.selectByExample(marketOrderExample);
        return orderList;
    }

    /**
     * 根据订单id获得订单中的商品
     *
     * @param orderId 订单id
     * @return 订单中的商品信息
     */
    private List<MarketOrderGoods> getOrderGoodsList(Integer orderId) {
        List<MarketOrderGoods> orderGoodsList;
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        // 在订单商品表中查询商品 --> goodsList
        if (!StringUtil.isEmpty(String.valueOf(orderId))) {
            criteria.andOrderIdEqualTo(orderId);
        }
        orderGoodsList = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
        return orderGoodsList;
    }

    /**
     * 设置OrderListVo
     *
     * @param order          需要的订单
     * @param orderGoodsList 需要的订单商品表
     * @return List<OrderListVo> 返回订单信息
     */
    private OrderListVo setOrderListVo(MarketOrder order, List<MarketOrderGoods> orderGoodsList) {
        // TODO order_status 101 --> 未付款   102 --> 用户取消   103 --> 系统取消   201 --> 已付款
        //                   202 --> 申请退款  203 --> 已退款    301 --> 已发货    401 --> 用户收货  402 --> 系统收货
        // 设置基本参数
        OrderListVo orderListVo = new OrderListVo();
        String handelOption;
        switch (order.getOrderStatus()) {
            case 101:
                orderListVo.setOrderStatusText(this.getOrderStatusText(101));
                handelOption = WxOrderServiceImpl.property.getProperty("101");
                setOrderListVoField(orderListVo, order, orderGoodsList, handelOption);
                break;
            case 102:
                orderListVo.setOrderStatusText(this.getOrderStatusText(102));
                handelOption = WxOrderServiceImpl.property.getProperty("102");
                setOrderListVoField(orderListVo, order, orderGoodsList, handelOption);
                break;
            case 103:
                orderListVo.setOrderStatusText(this.getOrderStatusText(103));
                handelOption = WxOrderServiceImpl.property.getProperty("103");
                setOrderListVoField(orderListVo, order, orderGoodsList, handelOption);
                break;
            case 201:
                handelOption = WxOrderServiceImpl.property.getProperty("201");
                orderListVo.setOrderStatusText(this.getOrderStatusText(201));
                setOrderListVoField(orderListVo, order, orderGoodsList, handelOption);
                break;
            case 202:
                orderListVo.setOrderStatusText(this.getOrderStatusText(202));
                handelOption = WxOrderServiceImpl.property.getProperty("202");
                setOrderListVoField(orderListVo, order, orderGoodsList, handelOption);
                break;
            case 203:
                orderListVo.setOrderStatusText(this.getOrderStatusText(203));
                handelOption = WxOrderServiceImpl.property.getProperty("203");
                setOrderListVoField(orderListVo, order, orderGoodsList, handelOption);
                break;
            case 301:
                orderListVo.setOrderStatusText(this.getOrderStatusText(301));
                handelOption = WxOrderServiceImpl.property.getProperty("301");
                setOrderListVoField(orderListVo, order, orderGoodsList, handelOption);
                break;
            case 401:
                orderListVo.setOrderStatusText(this.getOrderStatusText(401));
                handelOption = WxOrderServiceImpl.property.getProperty("401");
                setOrderListVoField(orderListVo, order, orderGoodsList, handelOption);
                break;
            case 402:
                orderListVo.setOrderStatusText(this.getOrderStatusText(402));
                handelOption = WxOrderServiceImpl.property.getProperty("402");
                setOrderListVoField(orderListVo, order, orderGoodsList, handelOption);
                break;
            default:
        }
        return orderListVo;
    }

    /**
     * 给要返回的订单的成员变量复制
     *
     * @param orderListVo       要返回的订单
     * @param order             查询到的订单
     * @param orderGoodsList    查询到的商品
     * @param handleOptionValue 设置可执行操作的值
     */
    private void setOrderListVoField(OrderListVo orderListVo, MarketOrder order, List<MarketOrderGoods> orderGoodsList, String handleOptionValue) {
        this.setBasicField(orderListVo, order, orderGoodsList);
        this.setGoodList(orderListVo, orderGoodsList);
        WxOrderHandelOptionConfig wxOrderHandelOptionConfig = this.getHandleOption(handleOptionValue);
        orderListVo.setHandleOption(wxOrderHandelOptionConfig);
    }


    /**
     * 设置基础字段的值
     *
     * @param orderListVo    要设置的对象vo
     * @param order          订单
     * @param orderGoodsList 订单商品表
     */
    private void setBasicField(OrderListVo orderListVo, MarketOrder order, List<MarketOrderGoods> orderGoodsList) {
        // 设置是否售后
        orderListVo.setAftersaleStatus(order.getAftersaleStatus());
        // 设置是否团购
        if (!order.getGrouponPrice().equals(BigDecimal.valueOf(0))) {
            orderListVo.setGroupin(true);
        }
        // 设置订单号
        orderListVo.setOrderSn(order.getOrderSn());
        // 设置订单真实价格
        BigDecimal actualPrice = getActualPrice(order, orderGoodsList);
        orderListVo.setActualPrice(actualPrice);
        // 设置订单id
        orderListVo.setId(order.getId());
    }

    /**
     * 计算订单的真实价格
     *
     * @param order          查询到的订单
     * @param orderGoodsList 查询到的商品
     */
    private BigDecimal getActualPrice(MarketOrder order, List<MarketOrderGoods> orderGoodsList) {
        // TODO 订单费用 = orderPrice + FreightPrice - couponPrice
        // TODO 实际费用 actualPrice = orderPrice - integralPrice
        BigDecimal actualPrice = new BigDecimal(0);
        for (MarketOrderGoods orderGoods : orderGoodsList) {
            actualPrice = actualPrice.add(orderGoods.getPrice());
        }
        actualPrice = actualPrice.subtract(order.getCouponPrice());
        actualPrice = actualPrice.subtract(order.getIntegralPrice());
        actualPrice = actualPrice.subtract(order.getGrouponPrice());
        return actualPrice.add(order.getFreightPrice());
    }

    /**
     * 设置订单中的商品
     *
     * @param orderListVo    要设置的订单
     * @param orderGoodsList 查询到的商品
     */
    public void setGoodList(OrderListVo orderListVo, List<MarketOrderGoods> orderGoodsList) {
        List<OrderListVo.GoodsListBean> goodsList = orderListVo.getGoodsList();
        for (MarketOrderGoods orderGoods : orderGoodsList) {
            OrderListVo.GoodsListBean goodsListBean = new OrderListVo.GoodsListBean();
            goodsListBean.setNumber(Integer.parseInt(orderGoods.getNumber().toString()));
            goodsListBean.setPicUrl(orderGoods.getPicUrl());
            goodsListBean.setPrice(orderGoods.getPrice());
            goodsListBean.setId(orderGoods.getId());
            goodsListBean.setGoodsName(orderGoods.getGoodsName());
            String specifications = orderGoods.getSpecifications();
            String[] split = specifications.split(",");
            goodsListBean.setSpecifications(split);
            goodsList.add(goodsListBean);
        }
    }

    /**
     * 批量设置可操作的状态
     *
     * @param set 传入可操作的0和1组成的字符串
     */
    private WxOrderHandelOptionConfig getHandleOption(String set) {
        WxOrderHandelOptionConfig wxOrderHandelOptionConfig = new WxOrderHandelOptionConfig();
        try {
            Field[] declaredFields = wxOrderHandelOptionConfig.getClass().getDeclaredFields();
            if (set.length() != declaredFields.length) {
                throw new IllegalArgumentException("参数非法 ——> 设置条件不满足");
            }
            // 给每个字段赋值
            for (int i = 0; i < declaredFields.length; i++) {
                declaredFields[i].setAccessible(true);
                char c = set.charAt(i);
                if (c == '0') {
                    declaredFields[i].set(wxOrderHandelOptionConfig, false);
                } else if (c == '1') {
                    declaredFields[i].set(wxOrderHandelOptionConfig, true);
                } else {
                    throw new IllegalArgumentException("参数非法 ——> 无法赋值");
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
        return wxOrderHandelOptionConfig;
    }
}
